package com.zjsru.leetcode75.level1;

/**
 * @Author: cookLee
 * @Date: 2023-10-05
 * 最大连续1的个数 III
 */
public class LongestOnes {

    /**
     * 主
     * 输入：nums = [1,1,1,0,0,0,1,1,1,1,0], K = 2
     * 输出：6
     * 解释：[1,1,1,0,0,1,1,1,1,1,1]
     * 粗体数字从 0 翻转到 1，最长的子数组长度为 6。
     * \
     * 输入：nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3
     * 输出：10
     * 解释：[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
     * 粗体数字从 0 翻转到 1，最长的子数组长度为 10。
     * \
     * @param args args
     */
    public static void main(String[] args) {
        LongestOnes longestOnes = new LongestOnes();
        int[] nums = new int[]{1,1,1,0,0,0,1,1,1,1,0};
        int k = 2;
        System.out.println(longestOnes.longestOnes(nums, k));
    }

    public int longestOnes(int[] nums, int k) {
        //当前窗口内1的总数
        int count = 0;
        int left = 0, right = 0;
        while (right < nums.length) {
            count += nums[right];
            //检查窗口内0的个数是否超过了允许的k个
            if (right - left + 1 - count > k) {
                count -= nums[left];
                left++;
            }
            right++;
        }
        return right - left;
    }

}
